

package ipark.gui.mapeditor;

import java.awt.*;
import java.io.Serializable;
import javax.swing.ImageIcon;

/**
 * Passage component
 * @author jernej
 */
public class Passage extends MapComponent implements Serializable{
    
    private transient Image[] images = null;// new Image[4];
    private int typePassage = 0;
    private String[] passageTypes = {"2way Vertical","1way Vertical","2way Horizontal","1way Horizontal"};
    
    public Passage(){
      super();
      loadImages();
      try {
           MediaTracker tracker = new MediaTracker(this);
           for(int i=0;i<images.length;i++)
                tracker.addImage(images[i], 0);
           tracker.waitForID(0);
      } catch (Exception e) {}
      setSize(images[0].getWidth(this),images[0].getHeight(this));
      setName("Passage");
      setWidthMeters(4);
      setHeightMeters(10);
  }
    
    public Passage(Passage comp){
      this();
      setLocation(comp.getLocation());
      setSize(comp.getSize());
      setName(comp.getName());
      setWidthMeters(comp.getWidthMeters());
      setHeightMeters(comp.getHeightMeters());
      typePassage = comp.typePassage;
  }
    
    public void loadImages(){
        if(images==null){
            images = new Image[4];
            images[0] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/vertical2updown.png"));
            images[1] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/verticalupdown.png"));
            images[2] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/horizontal2updown.png"));
            images[3] = Toolkit.getDefaultToolkit().getImage(MapComponent.class.getResource("resources/horizontalupdown.png"));
        }
    }

    @Override
    public ImageIcon getIcon(int iWidth, int iHeight) {
        return new ImageIcon(images[0].getScaledInstance(iWidth,iHeight, Image.SCALE_SMOOTH));
    }

    @Override
    public boolean isHorizontalResizable() {
        return true;
    }

    @Override
    public boolean isVerticalResizable() {
        return true;
    }

    @Override
    public boolean isDiagonalResizable() {
        return false;
    }

    @Override
    public double minHeightMeters() {
        return 2;
    }

    @Override
    public double minWidthMeters() {
        return 2;
    }

    @Override
    public String[] getPropertyNames() {
        String[] ret = {"Name","Width","Height","Type"};
        return ret;
    }

    @Override
    public Object getPropertyValue(String name) {
      if(name.equals("Name"))
          return getName();
      if(name.equals("Width"))
          return Math.floor(getWidthMeters()*100)/100;
      if(name.equals("Height"))
          return Math.floor(getHeightMeters()*100)/100;
      if(name.equals("Type"))
          return passageTypes[typePassage];
      return null;
    }

    @Override
    public boolean setPropertyValue(String name, String value) {
        double temp;
      if(name.equals("Name")){
          setName(value);
          return true;
      }
      if(name.equals("Width")){
          try{
            temp = Double.parseDouble(value);
          }catch(NumberFormatException e){return false;}
          temp = Math.floor(temp*100)/100;
          if(temp<0.01 && temp >= minWidthMeters())
              return false;
          setWidthMeters(temp);
          setSize((int)(getHeightMeters()/getMetersPerPixel()),getWidth());
          return true;
      }
      if(name.equals("Height")){
          try{
            temp = Double.parseDouble(value);
          }catch(NumberFormatException e){return false;}
          temp = Math.floor(temp*100)/100;
          if(temp<0.01 && temp >= minHeightMeters())
              return false;
          setHeightMeters(temp);
          setSize(getWidth(),(int)(getHeightMeters()/getMetersPerPixel()));
          return true;
      }
      if(name.equals("Type")){
          for(int i=0;i<passageTypes.length;i++)
              if(passageTypes[i].equals(value)){
                  if((typePassage>1&&i<2)||(typePassage<2&&i>1)){
                      temp = getWidthMeters();
                      setWidthMeters(getHeightMeters());
                      setHeightMeters(temp);
                      setSize((int)(getWidthMeters()/getMetersPerPixel()),
                              (int)(getHeightMeters()/getMetersPerPixel()));
                  }
                  typePassage = i;
                  return true;
              }
          return false;
      }
      return false;
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        //izrišemo sliko, ki predstavlja ta objekt
        g.drawImage(images[typePassage], getLocation().x, getLocation().y,getLocation().x+getWidth(),
                    getLocation().y+getHeight(), 0, 0, images[typePassage].getWidth(this), images[typePassage].getHeight(this), this);
    }

    @Override
    public String[] getTypeNames(String property) {
        if(property.equals("Type"))      
            return passageTypes;
        else
            return null;
    }

    @Override
    public MapComponent duplicate() {
        MapComponent ret = new Passage(this);
        return ret;
    }
    
    @Override
    public boolean jointSide(int side) {
        switch(side){
            case WorkArea.JOINT_LEFT:
            case WorkArea.JOINT_RIGHT:
                if(typePassage<2)
                    return false;
                else
                    return true;
            case WorkArea.JOINT_TOP:
            case WorkArea.JOINT_BOTTOM:
                if(typePassage>1)
                    return false;
                else
                    return true;
            default:
                return false;
        }
    }

}
